package com.customview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2019/9/9 17:39
 */
public class CustomHistogram extends View {

    float Histogramheight;
    float Histogramwidth;
    RectF mRectF;
    float recfwidth;
    float recfheight;
    Paint mpaint;
    Path path;
    int rectSize=9;
    int rectSpan=20;
    float rectRadius=20;
    float mPercent;

    ValueAnimator mAnimator;

    public CustomHistogram(Context context) {
        this(context, null);
    }

    public CustomHistogram(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomHistogram(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {
        mpaint = new Paint();
        mpaint.setStyle(Paint.Style.FILL);
        mpaint.setColor(Color.RED);
        mRectF = new RectF();

        path = new Path();

        startAnimator(0,10,7000);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float width = getMeasuredWidth();
        float height = getMeasuredHeight();
        recfwidth = (width -rectSize*rectSpan)/rectSize;
        rectRadius=recfwidth/2;
        recfheight = height;
        for (int i = 0; i <rectSize ; i++) {
            mRectF.set(i * recfwidth + 20, recfheight*mPercent, (i + 1) * recfwidth, recfheight);
            float[] floats = {rectRadius, rectRadius, rectRadius, rectRadius, rectRadius, rectRadius, rectRadius, rectRadius};
            path.addRoundRect(mRectF,floats,Path.Direction.CCW);
            canvas.drawPath(path,mpaint);
        }


    }


    private void startAnimator(float start, float end, final long animTime) {
        mAnimator = ValueAnimator.ofFloat(0, animTime);
        mAnimator.setDuration(animTime);
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mPercent = (animTime-((float) animation.getAnimatedValue()))/animTime;
//                mValue = mPercent * mMaxValue;

/*                if (BuildConfig.DEBUG) {
                    Log.d(TAG, "onAnimationUpdate: percent = " + mPercent
                            + ";currentAngle = " + (mSweepAngle * mPercent)
                            + ";value = " + mValue);
                }*/
                invalidate();
            }
        });
        mAnimator.start();
    }


}
